<%--
  Created by IntelliJ IDEA.
  User: 王韫琛
  Date: 2021/1/10
  Time: 2:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>住户信息添加</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/2.5.6/css/layui.css">
</head>
<body>
<div style="text-align: center;">
    <br>
    <fieldset class="layui-elem-field layui-field-title">
        <legend>住户信息添加</legend>
    </fieldset>
    <br>
    <div class="layui-inline">
        <form action="${pageContext.request.contextPath}/register" class="layui-form layui-form-pane" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label" for="name">姓名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入用户姓名" placeholder="请输入用户名" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="gender">性别:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="gender" id="gender" value="男" title="男" checked="checked">
                    <input type="radio" name="gender" value="女" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="age">年龄:</label>
                <div class="layui-input-inline">
                    <input type="text" name="age" id="age" class="layui-input" required lay-verify="required|number"
                           lay-reqText="请输入年龄"
                           placeholder="请输入年龄" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="phone">手机号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" id="phone" class="layui-input" required lay-verify="required|phone"
                           lay-reqText="请输入正确的手机号" placeholder="请输入手机号" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="identity_card">身份证号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="identity_card" id="identity_card" class="layui-input" required lay-verify="required|identity"
                           lay-reqText="身份证号" placeholder="身份证号" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"for="census">籍贯:</label>
                <div class="layui-input-inline">
                    <input type="text" name="census" id="census" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入籍贯" placeholder="请输入籍贯" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="nat">核酸证明:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="nat" id="nat" value="有" title="有" checked="checked">
                    <input type="radio" name="nat" value="无" title="无">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="bus_type">交通工具:</label>
                <div class="layui-input-inline">
                    <input type="text" name="bus_type" id="bus_type" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入交通工具" placeholder="请输入交通工具" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="ticket">班次</label>
                <div class="layui-input-inline">
                    <input type="text" name="ticket" id="ticket" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入班次" placeholder="请输入班次" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="interchange">中转站:</label>
                <div class="layui-input-inline">
                    <input type="text" name="interchange" id="interchange" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入中转站" placeholder="请输入中转站" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="sum_family">家庭人数:</label>
                <div class="layui-input-inline">
                    <input type="text" name="sum_family" id="sum_family" class="layui-input" required lay-verify="required|number"
                           lay-reqText="请输入家庭总人数" placeholder="请输入家庭总人数" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="test1">添加时间:</label>
                <div class="layui-input-inline">
                    <%--<input type="text" name="addtime" class="layui-input" required lay-verify="required|date"
                           lay-reqText="请输入家庭人数" placeholder="请输入家庭人数" autocomplete="off">--%>
                    <input type="text" name="addtime" required lay-verify="required|data" class="layui-input" id="test1">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label" for="remark">备注:</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="remark" name="remark" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-inline" style="padding-left: 45px">
                    <button class="layui-btn" lay-submit lay-filter="doSubmit" lay-event="add">注册</button>
                    <button type="reset" class="layui-btn layui-btn-warm">重置</button>
                </div>
            </div>

        </form>
    </div>
</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/layui/2.5.6/layui.js"></script>
<script>
    layui.use(['layer','form','laydate','jquery'], function () {
        //定义变量
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.jquery;
        //指定时间
        var laydate = layui.laydate;
        var url = "/register";
        //执行一个laydate实例
        laydate.render({
            elem: '#test1' //指定元素
            ,format: 'yyyy-MM-dd HH:mm:ss'
        });
        //标识是否存在
        var flag = false;
        //验证身份证是否存在
        $("#identity_card").blur(function () {
            //获取输入的用户名
            let identity_card = $(this).val().trim();
            //判断是否为空
            if (identity_card.length>0){
                $.get("/selectByIdentityCard",{"identity_card":identity_card},function (result) {
                    if (result.success){
                        //layer.msg(result.message);
                        layer.tips(result.message,"#identity_card");
                        flag = true;
                    }else {
                        flag = false;
                        layer.tips(result.message,"#identity_card");
                    }
                },"json");
            }
        });
        //验证手机号是否存在
        $("#phone").blur(function () {
            let phone = $(this).val().trim();
            //判断是否为空
            if (phone.length>0){
                $.get("/selectByPhone",{"phone":phone},function (result) {
                    if (result.success){
                        //layer.msg(result.message);
                        layer.tips(result.message,"#phone");
                        flag = true;
                    }else {
                        flag = false;
                        layer.tips(result.message,"#phone");
                    }
                },"json");
            }
        })
        //表单监听事件
        form.on("submit(doSubmit)", function (data) {
            if (flag){
                layer.msg("身份证已经存在,请重新输入");
            }else {
                $.post(url, data.field, function (result) {
                    if (result.success) {
                        layer.alert(result.message,{icon: 1},function (index) {
                            location.href="/house_Exhibiting_Infor";
                            layer.close(index);
                        });
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }, "json");
            }
            //禁止页面跳转
            return false;
        });
    });
</script>
</body>
</html>